// pages/live/index.js
Page({
  data: {
    lives: [
      {
        id: 1,
        title: "成都春熙路美食探店",
        cover: "/assets/img/message/mountain.png",
        streamer: "美食达人",
        viewers: 1234,
        status: "live",
        category: "美食",
        description: "带大家探索春熙路的美食小店",
        isFollowed: false
      },
      {
        id: 2,
        title: "城市夜景摄影教学",
        cover: "/assets/img/message/phone.png",
        streamer: "摄影爱好者",
        viewers: 856,
        status: "live",
        category: "摄影",
        description: "教你如何拍摄美丽的城市夜景",
        isFollowed: false
      },
      {
        id: 3,
        title: "周末徒步路线分享",
        cover: "/assets/img/message/draw.png",
        streamer: "户外达人",
        viewers: 567,
        status: "live",
        category: "户外",
        description: "分享几条适合周末的徒步路线",
        isFollowed: false
      },
      {
        id: 4,
        title: "城市文化历史讲解",
        cover: "/assets/img/message/computer.png",
        streamer: "历史学者",
        viewers: 234,
        status: "ended",
        category: "文化",
        description: "深入了解这座城市的历史文化",
        isFollowed: false
      }
    ],
    categories: [
      { name: "全部", active: true },
      { name: "美食", active: false },
      { name: "摄影", active: false },
      { name: "户外", active: false },
      { name: "文化", active: false }
    ]
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '城市直播'
    })
    
    // 从本地存储中读取已关注的直播信息
    const followedLives = wx.getStorageSync('followedLives') || []
    const lives = this.data.lives.map(live => {
      const isFollowed = followedLives.some(followedLive => followedLive.id === live.id)
      return { ...live, isFollowed }
    })
    this.setData({ lives })
  },

  onLiveTap: function(e) {
    const live = e.currentTarget.dataset.live
    
    if (live.status === "ended") {
      wx.showToast({
        title: '直播已结束',
        icon: 'none'
      })
      return
    }
    
    wx.showModal({
      title: live.title,
      content: `主播：${live.streamer}\n分类：${live.category}\n观看人数：${live.viewers}\n状态：${live.status === 'live' ? '直播中' : '已结束'}\n\n${live.description}`,
      showCancel: true,
      cancelText: '关闭',
      confirmText: '进入直播间',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '直播间功能开发中',
            icon: 'none'
          })
        }
      }
    })
  },

  onCategoryTap: function(e) {
    const index = e.currentTarget.dataset.index
    const categories = this.data.categories.map((item, i) => {
      item.active = i === index
      return item
    })
    this.setData({ categories })
    
    wx.showToast({
      title: '筛选：' + categories[index].name,
      icon: 'none'
    })
  },

  onFollowTap: function(e) {
    const index = e.currentTarget.dataset.index
    const lives = this.data.lives
    lives[index].isFollowed = !lives[index].isFollowed
    this.setData({ lives }, () => {
      // 存储已关注的直播到本地
      const followedLives = lives.filter(live => live.isFollowed)
      wx.setStorageSync('followedLives', followedLives)
      console.log('关注列表已更新:', followedLives)
      // 触发圈子页面更新
      if (getCurrentPages().some(page => page.route === 'pages/circle/index')) {
        const circlePage = getCurrentPages().find(page => page.route === 'pages/circle/index')
        circlePage.onLoad()
      }
    })
  },
  onStartLiveTap:function() {
    wx.showModal({
      title: '开始直播',
      content: '确定要开始直播吗？',
      success: function(res) {
        if (res.confirm) {
          wx.showToast({
            title: '直播功能开发中',
            icon: 'none'
          })
        }
      }
    })
  }
})
